﻿@page "/producttype1"
@using System.Collections.ObjectModel
@inject DialogService DialogService
@inject IProductService ProductService

<h1>商品分类</h1>

<RadzenStack Style="display:flex" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start">
    <RadzenTextBox @bind-Value="condition" Placeholder="分类" Style="margin:10px" />
    <RadzenButton Text="查询" Icon="refresh" Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px" />
    <RadzenButton Text="新增" Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px" />
</RadzenStack>

<RadzenDataGrid TItem="cts_ProductType" Data="@ProductTypes" GridLines="Radzen.DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn TItem="cts_ProductType" Property="Id" Title="序号" Width="100px" TextAlign="TextAlign.Center" />
        <RadzenDataGridColumn TItem="cts_ProductType" Property="" Title="操作" Width="260px">
            <Template Context="data">
                <RadzenStack Gap="10" Style="margin-bottom: 10px" Orientation="Orientation.Horizontal">
                    <RadzenButton Text="修改" Disabled="@(data.ParentId == 0)" @onclick:stopPropagation Click="@(args=> Update(data))" ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Text="删除" Disabled="@(data.ParentId == 0)" @onclick:stopPropagation Click="@(args=> Delete(data))" ButtonStyle="ButtonStyle.Danger" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="cts_ProductType" Property="Name" Title="名称" />
        <RadzenDataGridColumn TItem="cts_ProductType" Property="Sort" Title="排序" />
    </Columns>
</RadzenDataGrid>

@code {
    ObservableCollection<cts_ProductType>? ProductTypes;

    private string? condition = "";

    private void Search()
    {
        BaseInput input = new BaseInput(){
            keyword = condition!,
        };
        var list = ProductService.SearchProductType(input);        
        ProductTypes = new ObservableCollection<cts_ProductType>(list!);
    }

    private async void Add()
    {
        var productType = new cts_ProductType();
        var result = await DialogService.OpenAsync<ProductTypeAddDialog>($"新增商品分类",
               new Dictionary<string, object>() { { "PProductType", productType } },
               new DialogOptions()
                   {
                       Resizable = true,
                       Draggable = true,
                       Resize = OnResize,
                       Drag = OnDrag,
                       Width = "700px",
                       Height = "382px",
                       Left = null,
                       Top = null
                   });

        if (result != null){
            ProductTypes?.Add(result);
            await DialogService.Alert("保存成功。", "提示信息", new AlertOptions() { OkButtonText = "确定" });
        }
    }

    private async void Update(cts_ProductType selectedItem)
    {
        var copy = new cts_ProductType()
            {
                CreatedTime = selectedItem.CreatedTime,
                CreatedUserId = selectedItem.CreatedUserId,
                CreatedUserName = selectedItem.CreatedUserName,
                Id = selectedItem.Id,
                IsDeleted = selectedItem.IsDeleted,
                Name = selectedItem.Name,
                ParentId = selectedItem.ParentId,
                ParentIds = selectedItem.ParentIds,
                Products = selectedItem.Products,
                Sort = selectedItem.Sort,
                TenantId = selectedItem.TenantId,
                UpdatedTime = selectedItem.UpdatedTime,
                UpdatedUserId = selectedItem.UpdatedUserId,
                UpdatedUserName = selectedItem.UpdatedUserName,
            };

        var result = await DialogService.OpenAsync<ProductTypeAddDialog>($"修改商品分类",
                      new Dictionary<string, object>() { { "PProductType", copy } },
                      new DialogOptions()
                          {
                              Resizable = true,
                              Draggable = true,
                              Resize = OnResize,
                              Drag = OnDrag,
                              Width = "700px",
                              Height = "382px",
                              Left = null,
                              Top = null
                          });
        if (result != null)
        {                        
            selectedItem.CreatedTime = result.CreatedTime;
            selectedItem.CreatedUserId = result.CreatedUserId;
            selectedItem.CreatedUserName = result.CreatedUserName;
            selectedItem.Id = result.Id;
            selectedItem.IsDeleted = result.IsDeleted;
            selectedItem.Name = result.Name;
            selectedItem.ParentId = result.ParentId;
            selectedItem.ParentIds = result.ParentIds;
            selectedItem.Products = result.Products;
            selectedItem.Sort = result.Sort;
            selectedItem.TenantId = result.TenantId;
            selectedItem.UpdatedTime = result.UpdatedTime;
            selectedItem.UpdatedUserId = result.UpdatedUserId;
            selectedItem.UpdatedUserName = result.UpdatedUserName;
            StateHasChanged();
        }
    }

    private async void Delete(cts_ProductType productType)
    {
        var dialogResult = await DialogService.Confirm("确定要删除选择的商品分类吗？", "删除", new ConfirmOptions() 
        { 
           OkButtonText = "确定", 
           CancelButtonText = "取消" 
        });

        if ((bool) dialogResult)
        {
            try
            {
                ProductService.DeleteProductType(productType.Id);
                await DialogService.Alert("删除成功。", "提示信息", new AlertOptions() { OkButtonText = "确定" });
                ProductTypes?.Remove(productType);
            }
            catch (Exception ex)
            {
                await DialogService.Alert(ex.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
                return;
            }
        }
    }

    void OnDrag(System.Drawing.Point point)
    {
    }

    void OnResize(System.Drawing.Size size)
    {
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        var list = ProductService.SearchProductType(new BaseInput() { keyword = "" });
        ProductTypes = new ObservableCollection<cts_ProductType>(list!);
    }

}

